package com.javalab._7thread._4ThreadLocal;

/**
 * 打印机类
 */
public class Printer {
    private static final ThreadLocal<String> THREAD_LOCAL_NAME = new ThreadLocal<>();
    private static final ThreadLocal<FlashUSB> THREAD_LOCAL_USB = new ThreadLocal<>();

    public static void insertUSBFlash(String name, FlashUSB flashUSB) {
        THREAD_LOCAL_NAME.set(name);
        THREAD_LOCAL_USB.set(flashUSB);
        System.out.println(name + "插入USB " + flashUSB.toString());
    }

    public static void removeUSBFlash() {
        String name = THREAD_LOCAL_NAME.get();
        FlashUSB flashUSB = THREAD_LOCAL_USB.get();
        if (flashUSB != null && name != null) {
            System.out.println(name + "拔出USB " + flashUSB.toString());
        }

        THREAD_LOCAL_NAME.remove();
        THREAD_LOCAL_USB.remove();
    }

    public static void printA4() {
        String name = THREAD_LOCAL_NAME.get();
        FlashUSB usb = THREAD_LOCAL_USB.get();
        if (usb == null) {
            System.out.println("没有插入usb");
            return;
        }

        // 读取usb中的文字
        String text = usb.getText();
        System.out.println(name + "打印A4纸，内容=>" + text);
    }

    public static void printA3() {
        String name = THREAD_LOCAL_NAME.get();
        FlashUSB usb = THREAD_LOCAL_USB.get();
        if (usb == null) {
            System.out.println("没有插入usb");
            return;
        }

        // 读取usb中的文字
        String text = usb.getText();
        System.out.println(name + "打印A3纸，内容=>" + text);
    }
}
